package com.lee.wordcounter.utils;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;

public class WordCounterUtils {
	public static final String testFileName = "test1.txt";
	/**
	 * @return a string with length ranging from 3 to 8 random chars
	 */
	public static String generateRandomWord() {
		Random rand = new Random();
		char [] word = new char[rand.nextInt(5) + 3];
		for (int i =0 ;i < word.length; i++) {
			word [i] = (char) ('a' + rand.nextInt(26));
		}
		
		return new String(word);
	}
	
	public static void generateRandomTextFile(String fileName) {
	    generateRandomTextFile(fileName, 200, 40);
	}
	

	/**
	 * Generates a file with the supplied name containing random words.
	 * @param fileName 
	 * @param lineNum Number of lines to be generated 
	 * @param numWordsPerLine Number of words to be generated per line
	 */
	public static void generateRandomTextFile(String fileName, long lineNum, long numWordsPerLine) {
		try {
			FileOutputStream os = new FileOutputStream(fileName);
			PrintWriter pw = new PrintWriter(os);
			int lines = 0;
			while (lines < lineNum) {
				int index = 0;
				while (index < numWordsPerLine) {
					pw.print(generateRandomWord());
					pw.print(" ");
					index ++;
				}
				pw.println();
				lines ++;
			}
			
			pw.close();
			os.close();
			
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public static void main (String [] args) {
		WordCounterUtils.generateRandomTextFile(testFileName, 1000, 40);
	}
}
